package com.richard.dev.common.mvvm.common;


import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Richard
 * @createDate: 2023/4/7 14:36
 * @version: 1.0
 * @description: 自定义ObservableField
 */
public class XObservableField<T> extends ObservableField<T> {

    private static final long serialVersionUID = 4469180065137877432L;
    private final List<OnPropertyChangedCallback> callbacks = new ArrayList<>();

    @Override
    public void addOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
        super.addOnPropertyChangedCallback(callback);
        callbacks.add(callback);
    }

    @Override
    public void removeOnPropertyChangedCallback(@NonNull OnPropertyChangedCallback callback) {
        super.removeOnPropertyChangedCallback(callback);
        callbacks.remove(callback);
    }

    /**
     * 删除全部callback
     */
    public void removeAllOnPropertyChangedCallback() {
        for (OnPropertyChangedCallback item : callbacks) {
            this.removeOnPropertyChangedCallback(item);
        }
    }
}

